//
//  CallIncomingListenner.m
//  xiuqu
//
//  Created by shuihulu02 on 2016/12/16.
//  Copyright © 2016年 ASASDA. All rights reserved.
//

#import "CallIncomingListenner.h"
#import "CallRecvViewController.h"
#import "AppDelegate.h"
#import <UIViewController+TopViewController.h>
#import "CallMakeViewController.h"
#import "XLV2LoginViewController.h"
#import "XLRecVoiceChatVC.h"
@interface CallIncomingListenner ()
@property (nonatomic, assign) BOOL receivedCall;
@property (nonatomic, strong) TILCallInvitation *invitation;
@end

@implementation CallIncomingListenner
- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationDidBecomeActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelCall) name:@"ontherPersonCause" object:nil];
    }
    return self;
}

#pragma mark --- 接受电话时收到的im的信息
- (void)onC2CCallInvitation:(TILCallInvitation*)invitation;
{
    if ([[UIViewController topViewController] isKindOfClass:[CallRecvViewController class]] || [[UIViewController topViewController] isKindOfClass:[CallMakeViewController class]] || [[UIViewController topViewController] isKindOfClass:[XLV2LoginViewController class]]) {
        return;
    }
    
    //接听超时
    if ([[NSDate date] timeIntervalSinceDate:invitation.inviteDate] > 60) {
        return;
    }
    
    [[[UIApplication sharedApplication].keyWindow viewWithTag:1234567890] removeFromSuperview];
    if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
            if (invitation.callType == 1) {//音频
                XLRecVoiceChatVC *call = [[XLRecVoiceChatVC alloc] init];//接听
                call.peerId = invitation.sponsorId;
                call.invite = invitation;
                call.callid = [NSString stringWithFormat:@"%d", self.invitation.callId];
                UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:call];
                [[UIViewController topViewController] presentViewController:nav animated:YES completion:nil];
            }else{//视频
                CallRecvViewController *call = [[CallRecvViewController alloc] init];//接听
                call.peerId = invitation.sponsorId;
                call.invite = invitation;
                call.callid = [NSString stringWithFormat:@"%d", self.invitation.callId];
                [kAppWindow bringSubviewToFront:call];
                UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:call];
                [[UIViewController topViewController] presentViewController:nav animated:YES completion:nil];
                
            }
    }else{
        self.invitation = invitation;
        self.receivedCall = YES;
    }
}

- (void)applicationWillEnterForeground:(NSNotification *)notification {
    if (self.receivedCall) {
        self.receivedCall = NO;
        if (self.invitation.callType == 1) {//音频
            XLRecVoiceChatVC *call = [[XLRecVoiceChatVC alloc] init];//接听
            call.peerId = self.invitation.sponsorId;
            call.invite = self.invitation;
            call.callid = [NSString stringWithFormat:@"%d", self.invitation.callId];
            UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:call];
            [[UIViewController topViewController] presentViewController:nav animated:YES completion:nil];
        }else{//视频
            CallRecvViewController *callVC = [[CallRecvViewController alloc] init];//接听
            callVC.peerId = self.invitation.sponsorId;
            callVC.invite = self.invitation;
            callVC.callid = [NSString stringWithFormat:@"%d", self.invitation.callId];
            UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:callVC];
            [[UIViewController topViewController] presentViewController:nav animated:YES completion:nil];
        }
    }
}

- (void)cancelCall {
    if (self.receivedCall) {
        self.receivedCall = NO;
    }
}

@end
